<template>
  <div>
    <div ref="chartBox" class="chartBox"></div>
    <el-switch
      v-model="value1"
      active-text="开启数据随机变化"
      inactive-text="关闭数据变化"
      @click="!value1"
    >
    </el-switch>
  </div>
</template>

<script>
export default {
  name: 'Chart',
  data () {
    return {
      value1: true
    }
  },
  watch: {
    value1 (newValue) {
      if (newValue === false) {
        clearInterval(this.timer)
      } else {
        this.setTimer()
      }
    }
  },
  mounted () {
    this.initEchart()
  },
  destroyed () {
    // 组件销毁清除定时器
    clearInterval(this.timer)
  },
  methods: {
    initEchart () {
      // 基于准备好的dom，初始化echarts实例
      this.myChart = this.echarts.init(this.$refs.chartBox)
      this.opt = {
        tooltip: {},
        radar: {
          // shape: 'circle',
          // 维度（范围）
          indicator: [
            { name: '工作效率', max: 100 },
            { name: '考勤', max: 100 },
            { name: '积极性', max: 100 },
            { name: '帮助同事', max: 100 },
            { name: '自主学习', max: 100 },
            { name: '正确率', max: 100 }
          ]
        },
        series: [
          {
            // 表格类型
            type: 'radar',
            data: [
              {
                value: [10, 1, 100, 5, 100, 0],
                name: '张三'
              },
              {
                value: [50, 50, 50, 50, 50, 10],
                name: '李四'
              }
            ]
          }
        ]
      }
      // 绘制图表
      this.myChart.setOption(this.opt)
      this.setTimer()
    },
    setTimer () {
      this.timer = setInterval(() => {
        this.getData()
      }, 1000)
    },
    getData () {
      const data = []
      for (let index = 0; index < 2; index++) {
        const per = { value: [], name: `${index}` }
        for (let j = 0; j < 6; j++) {
          const num = Math.floor(Math.random() * 100)
          per.value.push(num)
        }
        data.push(per)
      }
      this.opt.series[0].data = data
      this.myChart.setOption(this.opt)
    }
  }
}
</script>

<style lang="scss" scoped>
.chartBox {
  width: 100%;
  height: 400px;
}
</style>
